local zuixiang = fk.CreateSkill {
  name = "lvshi__zuixiang",
}

Fk:loadTranslationTable{
  ["lvshi__zuixiang"] = "醉乡",
  ["@zuixiang_drank"] = "醉乡",
  [":lvshi__zuixiang"] = "你的【酒】状态不会因回合结束而消失。当你成为其他角色使用黑色牌的目标后，若你未处于【酒】状态，你可明置一张牌，视为使用【酒】，"..
  "否则你可弃置一张明置牌或装备区内的牌，令此牌对你无效。",
  ["#lvshi__zuixiang-invoke"] = "醉乡：你可以明置一张牌，视为使用【酒】",
  ["#lvshi__zuixiang-card"] = "醉乡：你可以弃置一张明置牌，令%arg对你无效",
  
  ["$lvshi__zuixiang1"] = "歌一曲，酒一觞，邀君醉琼浆，东风盈暖房。",
  ["$lvshi__zuixiang2"] = "醒又醉，醉复眠，迟日江山如画，何人不流连。",
}
local DIY = require "packages/diy_utility/diy_utility"

zuixiang:addEffect(fk.TargetConfirmed, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zuixiang.name) and data.card.color == Card.Black and data.from ~= player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if player.drank + player:getMark("@zuixiang_drank") == 0 then
      local hidden = table.filter(player:getCardIds("h"), function (id)
        return not table.contains(DIY.getShownCards(player), id)
      end)
      local params = {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = zuixiang.name,
        cancelable = true,
        pattern = tostring(Exppattern{ id = hidden }),
        prompt = "#lvshi__zuixiang-invoke"
        }
        local showCard = room:askToCards(player, params)
        if #showCard > 0 then
          event:setCostData(self, {cards = showCard})
          return true
        end
    else
      local ids = table.simpleClone(DIY.getShownCards(player))
      table.insertTable(ids, table.simpleClone(player:getCardIds("e")))
      local card = room:askToDiscard(player, {
        skill_name = zuixiang.name,
        min_num = 1,
        max_num = 1,
        include_equip = true,
        pattern = tostring(Exppattern{ id = ids }),
        prompt = "#lvshi__zuixiang-card:::"..data.card:toLogString(),
        cancelable = true,
      })
      if #card > 0 then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.drank + player:getMark("@zuixiang_drank") == 0 then
      DIY.showCards(player, event:getCostData(self).cards)
      if player.dead then return end
      room:useVirtualCard("analeptic", nil, player, player, zuixiang.name, true)
    else
      data.use.nullifiedTargets = data.use.nullifiedTargets or {}
      table.insertIfNeed(data.use.nullifiedTargets, player)
    end
  end,
})

zuixiang:addEffect(fk.PreCardEffect, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(zuixiang.name) and data.to == player and
      data.card.name == "analeptic" and
      not (data.extra_data and data.extra_data.analepticRecover)
  end,
  on_refresh = function(self, event, target, player, data)
    local card = data.card:clone()
    local c = table.simpleClone(data.card)
    for k, v in pairs(c) do
      card[k] = v
    end
    card.skill = Fk.skills["zuixiang__analeptic_skill"]
    data.card = card
  end,
})

zuixiang:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and data.card.trueName == "slash" and player:getMark("@zuixiang_drank") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.additionalDamage = (data.additionalDamage or 0) + player:getMark("@zuixiang_drank")
    data.extra_data = data.extra_data or {}
    data.extra_data.drankBuff = player:getMark("@zuixiang_drank")
    player.room:setPlayerMark(player, "@zuixiang_drank", 0)
  end,
})

return zuixiang
